package it.ap.sa.acqua.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import it.ap.sa.acqua.dao.model.TFish;
import it.ap.sa.acqua.dao.model.TUser;
import it.ap.sa.core.dao.util.AbstractGenericEntityDao;
import it.ap.sa.core.utils.LogUtil;
/**
 * @author Alessandro Paolillo
 */
@Repository
public class TUserDao  extends AbstractGenericEntityDao<TUser>{

	/**
	 * logger
	 */
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public TUser findById(Long id) {
		try{
			String  hql     = "from " + TUser.class.getName() + " where id = :id";
			Session session = this.getSessionFactory().getCurrentSession();
			Query   query   = session.createQuery(hql);

			query.setLong("id", id);

			return (TUser)query.uniqueResult();
		}catch(Exception e){
			if(logger.isDebugEnabled())
				logger.debug("Error in findById", e);
			throw e;
		}
    }
 
    public TUser findBySSO(String sso) {
        logger.info("SSO : {}", sso);
        try{
			String  hql     = "from " + TUser.class.getName() + " where sso_id = :sso";
			Session session = this.getSessionFactory().getCurrentSession();
			Query   query   = session.createQuery(hql);

			query.setString("sso", sso);

			return (TUser)query.uniqueResult();
		}catch(Exception e){
			if(logger.isDebugEnabled())
				logger.debug("Error in findBySSO", e);
			throw e;
		}
    }
 
    @SuppressWarnings("unchecked")
    public List<TUser> findAllUsers() {
        try{
			String  hql     = "from " + TUser.class.getName();
			Session session = this.getSessionFactory().getCurrentSession();
			Query   query   = session.createQuery(hql);

			return (List<TUser>)query.list();
		}catch(Exception e){
			if(logger.isDebugEnabled())
				logger.debug("Error in findAllUsers", e);
			throw e;
		}
    }
 
    /**
	 * @author Alessandro Paolillo
	 * @param {@link TFish} tFish
	 * @throws Exception
	 */
	@Transactional
	public TUser saveOrUpdate(TUser tUser) throws Exception {

		String    idLog     = "saveOrUpdate";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info(idLog);
		try {
			saveEntity(tUser);
		} catch (Exception ex) {

			logger.error("Si è verificato un errore in saveOrUpdate: " + ex.getMessage() , ex);
			throw(ex);
		} finally{

			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return tUser;
	}
 
    /**
	 */
	public void deleteBySSO(String sso){

		String    idLog     = "deleteBySSO";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info(idLog);
		try {

			if (sso!=null) {
				String sql = "DELETE FROM t_user WHERE sso_id = :sso";
				SQLQuery query = this.createSQLQuery(sql);
				query.setString("sso", sso);
				query.executeUpdate();
			}
		} catch (Exception ex) {

			logger.error("Si è verificato un errore in deleteBySSO: " + ex.getMessage() , ex);
			throw(ex);
		} finally{

			logger.info(LogUtil.endStopWatch(stopWatch));
		}
	}
}